package com.example.sportplay.controller;
import com.alibaba.fastjson.JSON;
import com.example.sportplay.bean.User;
import com.example.sportplay.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    UserDao userDao;

    @CrossOrigin
    @RequestMapping("/login")
    public String userLogin(@RequestBody User user) {
        System.out.println("已经开始预先");

        String flag = "error";
        String flag2 = "error";
        User userByMassage = userDao.getUserByMassage(user.getUsername(), user.getPassword());
        if (userByMassage!=null ) {
            flag = "ok2";
            if (userByMassage.getRole().equals("超级管理员") ) {
                flag = "ok3";
                HashMap<Object, Object> res = new HashMap<>();
                System.out.println(flag);
                res.put("flag",flag);
                res.put("role",userByMassage.getRole());
                res.put("user",userByMassage.getCarnumber());
                String s = JSON.toJSONString(res);

                return s;
            }
            HashMap<Object, Object> res = new HashMap<>();
            System.out.println(flag);
            res.put("flag",flag);
            res.put("role",userByMassage.getRole());
            res.put("user",userByMassage.getCarnumber());
            String s = JSON.toJSONString(res);

            return s;
        }

        HashMap<Object, Object> res = new HashMap<>();
        System.out.println(flag);
        res.put("flag",flag);

        String s = JSON.toJSONString(res);

        return s;

    }
}
